package org.angelica.modules.account.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.angelica.common.query.BaseQuery;
import org.angelica.common.wrapper.ResponseWrapper;
import org.angelica.core.response.ResponseResult;
import org.angelica.modules.account.entity.AccRoleDO;
import org.angelica.modules.account.entity.AccRolePermDO;
import org.angelica.modules.account.form.AccRoleCreateForm;
import org.angelica.modules.account.form.AccRoleUpdateForm;
import org.angelica.modules.account.service.AccRolePermService;
import org.angelica.modules.account.service.AccRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author aizhimin
 * @since 2019-05-13
 */
@RestController
@RequestMapping("api/account/accRole")
public class AccRoleController {

    @Autowired
    private AccRoleService accRoleService;
    @Autowired
    private AccRolePermService accRolePermService;

    @GetMapping("/")
    public ResponseResult listAccRole(BaseQuery baseQuery){
        IPage<AccRoleDO> accRolePage = accRoleService.page(baseQuery.buildPage());
        return ResponseResult.success("data", ResponseWrapper.wrapperPage(accRolePage));
    }

    /**
     * 页面初始化数据
     * @return
     */
    private Map<String,Object> initData(){
        BaseQuery baseQuery = new BaseQuery();
        IPage<AccRoleDO> accRolePage = accRoleService.page(baseQuery.buildPage());
        return ResponseWrapper.wrapperPage(accRolePage);
    }

    @GetMapping("/listAll")
    public ResponseResult listAllAccRole(){
        List<AccRoleDO> accRoleList = accRoleService.list();
        return ResponseResult.success("data", accRoleList);
    }

    @GetMapping("/listPermsByRoleId")
    public ResponseResult listPermsByRoleId(@RequestParam(value = "roleId")Long roleId){
        List<AccRolePermDO> accRolePermList = accRolePermService.list(new QueryWrapper<AccRolePermDO>()
                .select("perm_id")
                .eq("role_id",roleId));
        List<String> permIdList = accRolePermList.stream().map(accRolePermDO -> String.valueOf(accRolePermDO.getPermId())).collect(Collectors.toList());
        return ResponseResult.success("data", permIdList);
    }

    @PostMapping("/create")
    public ResponseResult createAccRole(@RequestBody @Valid AccRoleCreateForm accRoleCreateForm){
        accRoleService.insertRole(accRoleCreateForm);
        return ResponseResult.success("data",initData());
    }

    @PostMapping("/update")
    public ResponseResult updateAccRole(@RequestBody @Valid AccRoleUpdateForm accRoleUpdateForm){
        accRoleService.updateRole(accRoleUpdateForm);
        return ResponseResult.success("data",initData());
    }

    @GetMapping("/delete/{id}")
    public ResponseResult deleteAccRole(@PathVariable(value = "id")Long id){
        accRoleService.deleteRole(id);
        return ResponseResult.success("data",initData());
    }
}
